var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
formatTime = require("../../../component/common.js");
import Toast from '../../../component/vant/toast/toast';
import e from '../../../utils/httpPost.js';
Page({
  data: {
    show: false,
    friendApplyData: [],
    nearbyUserList:[],//附近的人
    wd:0,
    jd:0,
    isLoading:true,
    pageIndex:1,
    pageSize:10,
    from_type:'friend',
    keyword:'',
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.from_type = options.type;
    this.setData({
      from_type:options.type
    })
    if(options.type == 'friend'){
      this.onHttpFriendsRegisterList();
      wx.setNavigationBarTitle({
        title: '好友申请',
      })
    }else{
      this.data.pageIndex = 1;
      this.modalcnt();
      wx.setNavigationBarTitle({
        title: '附近的人',
      })
    }
  },
  onChange(val){
    console.log('onChange',val)
    this.data.keyword = val.detail;
    // this.initRequest()
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.data.pageIndex = 1;
    this.setData({
        nearbyUserList:[]
    })
    this.onHttpNearbyUserList()
  },
  modalcnt:function  ()  {
    var  that  =  this
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.wd = latitude//纬度
        that.data.jd = longitude//经度
        that.setData({
          wd : latitude,
          jd : longitude
        })
        that.onHttpNearbyUserList()
      },fail:function(res){
        that.data.wd = 0
        that.data.jd = 0
        that.onHttpNearbyUserList()
      }
    })
  },
  /**
   *  获取附近的人的用户列表
   */
  onHttpFriendsRegisterList: function () {
    var that = this;
    fetchApi.httpPost('/friend/friendsRegisterList', {
      estate_id: that.data.estate_id,
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize,
    }, function (res) {
      console.log("获取我的好友申请列表", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1;
        var friendList = res.data.data;
        if(friendList.length > 0){
          for (var index in friendList) {
            that.data.friendApplyData.splice(that.data.friendApplyData.length, 0, friendList[index]);
          }
          that.setData({
            hasData: true,
            friendApplyData: that.data.friendApplyData
          })
        }
      }else{
        Toast.fail(res.data.msg)
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  },
  /**
   *  获取附近的人的用户列表
   */
  onHttpNearbyUserList: function () {
    var that = this;
    fetchApi.httpPost('/friend/nearbyUserList', {
      keyword:that.data.keyword,
      longitude: that.data.jd,
      latitude: that.data.wd,
      pageIndex:that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("获取附近的人", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1;
        var nearbyUserList = res.data.data;
        if(nearbyUserList.length > 0){
          for (var index in nearbyUserList) {
            that.data.nearbyUserList.splice(that.data.nearbyUserList.length, 0, nearbyUserList[index]);
          }
          that.setData({
            hasData: true,
            nearbyUserList: that.data.nearbyUserList
          })
        }
      }else{
        Toast.fail(res.data.msg);
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  },
  addFriendReg(e){
    var friendId = e.currentTarget.dataset.friendId;
    console.log('friendId',friendId)
    this.onHttpAddFriendRegister(friendId);
  },
  /**
   *  获取附近的人的用户列表
   */
  onHttpAddFriendRegister(friend_id) {
    var that = this;
    fetchApi.httpPost('/friend/addFriendsRegister', {
      friend_id: friend_id,
    }, function (res) {
      console.log("添加好友申请反馈", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
      }else{
        Toast.fail(res.data.msg);
      }
    }, function () {
    })
  },
  addFriend(e){
    var regId = e.currentTarget.dataset.regId;
    var state = e.currentTarget.dataset.state;
    console.log('regId',regId)
    this.onHttpAddFriendSubmit(regId,state);
  },
  /**
   *  获取附近的人的用户列表
   */
  onHttpAddFriendSubmit(regId,state) {
    var that = this;
    fetchApi.httpPost('/friend/addFriendsSubmit', {
      register_id: regId,
      state:state,
    }, function (res) {
      console.log("添加好友反馈", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
      }else{
        Toast.fail(res.data.msg);
      }
      that.onPullDownRefresh();
    }, function () {
    })
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      friendApplyData: [],
      nearbyUserList:[],//附近的人
    })
    if(this.data.from_type == 'friend'){
      this.onHttpFriendsRegisterList();
    }else{
      this.modalcnt();
    }
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpLoadMore()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpLoadMore: function () {
    if(this.data.from_type == 'friend'){
      this.onHttpFriendsRegisterList();
    }else{
      this.modalcnt();
    }
  }
})